package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2016/4/27 0027.
 * describe:滑动开关按钮
 */
public class SlipButton extends View implements View.OnTouchListener{
    //当前按钮状态
    private boolean nowChoose=false;
    //用户是否在滑动
    private boolean onSlip=false;
    //按下时的X，当时的X
    private float downX,nowX;
    //打开和关闭状态下的，游标的Rect
    private Rect btn_On,btn_Off;

    private OnChangedListener ChgLsn;
    private boolean isChgLsnOn =false;

    private Bitmap bg_on,bg_off,slip_btn;
    public SlipButton(Context context) {
        super(context);
        init();
    }

    public SlipButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlipButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init()
    {
        bg_on= BitmapFactory.decodeResource(getResources(), R.drawable.switch_slider_normal);
        bg_off=BitmapFactory.decodeResource(getResources(), R.drawable.switch_state_mask);
        slip_btn=BitmapFactory.decodeResource(getResources(), R.drawable.switch_state_normal);
        int tmp=bg_off.getWidth()/2;
        btn_On=new Rect(tmp, 0, slip_btn.getWidth()+tmp, slip_btn.getHeight());
        btn_Off=new Rect(bg_off.getWidth()-tmp-slip_btn.getWidth(), 0, bg_off.getWidth()-tmp, slip_btn.getHeight());
        setOnTouchListener(this);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Matrix matrix=new Matrix();
        Paint paint=new Paint();
        float x;
        if(onSlip)
        {
            if(nowX>=bg_on.getWidth())
            {
                x=bg_on.getWidth()-slip_btn.getWidth()/2;
            }else{
                x=nowX-slip_btn.getWidth()/2;
            }
        }else{
            if(nowChoose)
            {
                x=btn_On.left;
            }else{
                x=btn_Off.left;
            }
        }
        if(nowX<(bg_on.getWidth()/2))
        {
            canvas.drawBitmap(bg_off, matrix, paint);
        }else{
            canvas.drawBitmap(bg_on, matrix, paint);
        }
        if(x<0)
        {
            x=0;
        }else if(x>bg_on.getWidth()-slip_btn.getWidth())
        {
            x=bg_on.getWidth()-slip_btn.getWidth();
        }
        canvas.drawBitmap(slip_btn, x, 0,paint);
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_MOVE:
                nowX=event.getX();
                break;
            case MotionEvent.ACTION_DOWN:
                if(event.getX()>bg_on.getWidth() || event.getY()>bg_on.getHeight())
                {
                    return false;
                }
                onSlip=true;
                downX=event.getX();
                nowX=downX;
                break;
            case MotionEvent.ACTION_UP:
                onSlip=false;
                boolean lastChoose=nowChoose;
                if(event.getX()>=(bg_on.getWidth()/2))
                {
                    nowChoose=true;
                }else{
                    nowChoose=false;
                }
                if(isChgLsnOn&&(lastChoose!=nowChoose))
                {
                    ChgLsn.OnChanged(nowChoose);
                }
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }
    public void setOnChangeListener(OnChangedListener l)
    {
        isChgLsnOn=true;
        ChgLsn=l;
    }

    public boolean isNowChoose()
    {
        return nowChoose;
    }

    public void setNowChoose(boolean nowChoose)
    {
        this.nowChoose = nowChoose;
        nowX=btn_On.left;
        invalidate();
    }
    public interface OnChangedListener
    {
        abstract void OnChanged(boolean CheckState);
    }


}
